package com.xscj.test.action;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;
import com.xscj.domain.Course;
import com.xscj.domain.Grade;
import com.xscj.service.CoursePlan;
import com.xscj.service.GradeSetUp;
import com.xscj.service.ScoreService;
import com.xscj.service.StuService;
import com.xscj.service.TeachingArrange;


public class ScoreEnterTestAction extends ActionSupport {

    /**
     *
     */
    private static final long serialVersionUID = 7103265542020117890L;

    @Autowired
    @Qualifier("teachingArrangeImpl")
    private TeachingArrange teachingArrange;

    @Autowired
    @Qualifier("gradeSetUpImpl")
    private GradeSetUp gradeSetUp;

    @Autowired
    @Qualifier("coursePlanImpl")
    private CoursePlan coursePlan;

    @Autowired
    @Qualifier("scoreServiceImpl")
    private ScoreService scoreService;

    @Autowired
    @Qualifier("stuServiceImpl")
    private StuService stuService;

    private String gradeID;
    private Grade grade;
    private int xueqi;
    private String courseID;
    private Course course;
    private String examType;
    private String result;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    @Override
    public String execute() throws Exception {
        int n = teachingArrange.isTrue(gradeID, xueqi, courseID);
        int m = scoreService.getCount(gradeID, xueqi, examType, courseID);
        int p = stuService.isExisits(gradeID);
        if (m >= p && n != 0) {
            n = -1;
        }
        grade = gradeSetUp.getGradeBybianHao(gradeID);
        course = coursePlan.getCourseByCourseID(courseID);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("flag", n);
        map.put("gradeName", grade.getYear() + "届（" + grade.getClassID() + "）班第" + xueqi + "学期");
        map.put("courseName", course.getName());
        JSONObject json = JSONObject.fromObject(map);
        result = json.toString();
        return SUCCESS;
    }

    public String getGradeID() {
        return gradeID;
    }

    public void setGradeID(String gradeID) {
        this.gradeID = gradeID;
    }

    public int getXueqi() {
        return xueqi;
    }

    public void setXueqi(int xueqi) {
        this.xueqi = xueqi;
    }

    public String getCourseID() {
        return courseID;
    }

    public void setCourseID(String courseID) {
        this.courseID = courseID;
    }

    public Grade getGrade() {
        return grade;
    }

    public void setGrade(Grade grade) {
        this.grade = grade;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public String getExamType() {
        return examType;
    }

    public void setExamType(String examType) {
        this.examType = examType;
    }

}
